package uit.edu.vn.Utility;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import uit.edu.vn.Activity.R;
import uit.edu.vn.Model.myImage;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageListAdapter extends BaseAdapter{

    private Context context;
    private List<myImage> listImage;

	private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
    public ImageListAdapter(Context context,List<myImage> listImage) {
    
        // TODO Auto-generated constructor stub
        this.context=context;
        this.listImage=listImage;
    	// thiêt lap mac dinh itemcheck là false
		for (int i = 0; i < this.getCount(); i++) {
		       itemChecked.add(i, false); // initializes all items value with false
		   }
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listImage.size();
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return listImage.get(position);
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        myImage entry = listImage.get(position);
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView =inflater.inflate(R.layout.row_image, null);
        
        TextView txt_ImageName= (TextView)convertView.findViewById(R.id.txt_ImageName);
        TextView txt_ImageSize= (TextView)convertView.findViewById(R.id.txt_ImageSize);
        ImageView txt_ImageView=(ImageView)convertView.findViewById(R.id.ImageView);
        // get data in imageName
        txt_ImageName.setText(entry.getName());
        //get data in ImageSize
        txt_ImageSize.setText(entry.getSize());
        // convert resource to bitmap
        String pathName=entry.getIcon();
        try {
            File imageFile = new File(pathName);
            Bitmap bmp;
            bmp=decodeFile(imageFile);
            txt_ImageView.setImageBitmap(bmp);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //xy lu check box 
		 CheckBox cbox= (CheckBox)convertView.findViewById(R.id.chkSelectImage);
		 cbox.setOnClickListener(new OnClickListener() {

		        public void onClick(View v) {

		            CheckBox cb = (CheckBox) v.findViewById(R.id.chkSelectImage);

		            if (cb.isChecked()) {
		                itemChecked.set(position, true);
	
		            } else if (!cb.isChecked()) {
		                itemChecked.set(position, false);
		
		            }
		        }
		    });

		cbox.setChecked(itemChecked.get(position));
		
        return convertView;
    }
    //Hàm decodefile nham tranh loi tran bo dem 
    private Bitmap decodeFile(File f){
        try {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);
             
            //Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE=70;
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){
                if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }
            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }
	// hàm lấy setcheck va get check de lay itemChecklist
	public ArrayList<Boolean> getCheckList()
	{
		return itemChecked;
	}
	public ArrayList<Boolean> setCheckList(ArrayList<Boolean> itemCheck)
	{
		itemChecked=itemCheck;
		return itemChecked;
	}
}  